!<!--  -->
<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>重置密码</span>
    </div>
    <el-form :model="pwdForm" :rules="pwdFormRules" ref="pwdFormRef" label-width="100px">
      <el-form-item label="原密码" prop="old_pwd">
        <el-input type="password" placeholder="请输入原密码" v-model="pwdForm.old_pwd"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input type="password" placeholder="请输入新密码" v-model="pwdForm.new_pwd"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="re_pwd">
        <el-input type="password" placeholder="请确认新密码" v-model="pwdForm.re_pwd"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitFn">修改密码</el-button>
        <el-button @click="resetFn">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { updateUserPwdAPI } from '@/api'

export default {
  name: 'userPwd',
  data() {
    // 新密码好旧密码不能相同
    const samePwd = (rules, value, callback) => {
      //value就是校验规则所对应输入框(新密码)
      if (this.pwdForm.old_pwd === value) {
        callback(new Error('新密码不能与旧密码一致'))
      } else {
        // 通过校验
        callback()
      }
    }
    const rePwd = (rules, value, callback) => {
      if (this.pwdForm.new_pwd != value) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        //校验通过
        callback()
      }
    }
    return {
      pwdForm: {

      },
      pwdFormRules: {
        old_pwd: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15的非空字符串',
            trigger: 'blur'
          }
        ],
        new_pwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15的非空字符串',
            trigger: 'blur'
          },
          // 自定义校验
          { validator: samePwd, trigger: 'blur' }
        ],
        re_pwd: [
          { required: true, message: '请输入确认密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15的非空字符串',
            trigger: 'blur'
          },
          { validator: rePwd, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitFn() {
      this.$refs.pwdFormRef.validate(valid => {
        if (valid) {
          // 在提交时校验，这时候会有两个弹窗，不太好，所以直接在数据输入时进行校验
          // if (this.new_pwd != this.re_pwd) {
          //   this.$message.error('两次新密码输入不一致')
          // }
          // if (this.old_pwd === this.new_pwd) {
          //   this.$message.error('新密码与旧密码一致')
          // }
          updateUserPwdAPI(this.pwdForm).then(res => {
            if (res.data.code == 0) {
              this.$message.success(res.data.message)
              // 重置密码成功后，强制退出
              // 清空token
              this.$store.commit('updateToken','')
              // 清空用户对象
              this.$store.commit('updateUserInfo',{})
              this.$router.push('/login')
            } else {
              this.$message.error('原密码不正确')
            }
          })
        }
      })
    },
    resetFn() {
       this.$refs.pwdFormRef.resetFields()
    }
  },

}
</script>

<style scoped>
.el-form {
  width: 500px;
}
</style>
